/*
 * @Description:
 * @Author: fhw
 * @Date: 2023-03-02 21:59:13
 */
import throttle, { DebouncedFunc } from '@/utils/tools/throttle'
import { isNil, map } from 'ramda'
import { Action, getModule, Module, Mutation, VuexModule } from 'vuex-module-decorators'
import store from '@/store'
import { UserState } from '@/views/user/list/uilts'
type Func = DebouncedFunc<(params: ObjectMap, options?: ObjectMap) => Promise<any>>
let getRole: Func
let getHomeUser: Func
export interface Options {
	value: string | number
	label: string
	type?: number
	title?: string
	[index: string]: any
	children?: Array<this>
	isLeaf?: boolean
	loading?: boolean
}
export interface BasicDataState {
	roleList: Array<Options>
	userList: Array<Options>
}

@Module({ namespaced: true, store, name: 'userData', dynamic: true })
export class UserData extends VuexModule implements BasicDataState {
	/** 角色 */
	public roleList: Array<Options> = []
	/** 系统用户 */
	public userList: Array<Options> = []
	/** 前台用户 */
	public homeUserList: Array<Options> = []

	/** 获取角色 */
	@Action
	public async getRoleList() {
		// if (isNil(getRole)) {
		// 	// 防抖
		// 	getRole = throttle(API.server.role.getRolePage.request)
		// }
		// const { success, data } = await getRole({ size: 999, current: 1 })
		// if (success && !isNil(data) && !isNil(data.data)) {
		// 	this.SET_GETROLELIST(data.data)
		// }
	}
	/** 获取前台用户 */
	@Action
	public async getHomeUserList() {
		if (isNil(getHomeUser)) {
			// 防抖
			getHomeUser = throttle(API.server.user.getUserPage.request)
		}
		const { success, data } = await getHomeUser({ pageSize: 99999999, pageNum: 1 })
		console.log(data);
		if (success && !isNil(data) && !isNil(data.data)) {

			this.SET_HOMEUSER(data.data)
		}
	}
	/** 获取前台用户 */
	@Mutation
	private SET_HOMEUSER(data: UserState[]) {
		this.homeUserList = map((res) => {
			return {
				...res,
				label: res.username as string,
				value: res.id,
			}
		}, data) as Options[]
	}
}

export const UserDataModule = getModule(UserData)
